package juc.ignore;

import java.util.concurrent.Exchanger;

/**
 * Exchanger可以用于线程间交换信息
 * 它提供一个同步点,当两个线程都到达这个同步点时,它们的信息交换
 * 只有一个到达时,它先等待,直到另一个线程也到达。
 *
 * @author 王霄
 * @date 2021/9/19 22:48
 */
public class ExchangerDemo {
    private final static Exchanger<String> exchanger = new Exchanger<>();

    public static void main(String[] args) {
        new Thread(() -> {
            try {
                System.out.println("bbb" + ":" + exchanger.exchange("hello,aaa"));
            } catch (InterruptedException ignored) {
            }
        }).start();

        new Thread(() -> {
            try {
                System.out.println("aaa" + ":" + exchanger.exchange("hello,bbb"));
            } catch (InterruptedException ignored) {
            }
        }).start();

    }
}
